﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Might.UIControls.PropertyEditor.Items {

    public partial class BoolItem : UserControl , IPropertyEditorItemWithValue<bool?> {
    
        private string label;
        private bool? value;
        private bool raiseEvents = false;
    
        public BoolItem( string label ) {        
            InitializeComponent();
            this.label = label;
            labelTxt.Text = label + " : ";
            valueCheckBox.Checked += new RoutedEventHandler( valueCheckBox_Changed );
            valueCheckBox.Unchecked += new RoutedEventHandler( valueCheckBox_Changed );
        }                       

        public event EventHandler ValueChanged;

        public string Label { get { return label; } }

        public bool? Value {
            get {
                return value;
            }
            set {
                raiseEvents = false;
                this.value = value;
                valueCheckBox.IsChecked = value;
                raiseEvents = true;                              
            }
        }

        protected void onValueChanged( EventArgs e ) {
            if( ValueChanged != null && raiseEvents ) {
                value = valueCheckBox.IsChecked;
                ValueChanged( this , e );
            }
        }

        void valueCheckBox_Changed( object sender , RoutedEventArgs e ) {
            onValueChanged( null );
        }         
        
    }
}
